﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Repositories;
using GameSchoolApplication.Models;
using GameSchoolApplication.Models.ViewModels;

namespace GameSchoolApplication.Controllers
{

        

    public class CourseController : Controller
    {

        /// <summary>
        /// Returns the course index for all of the user's courses
        /// Precondition: the user has navigated to the front page
        /// Postcondition: The user has received the overview view
        /// Author: Bjarmi
        /// Date: 7.5
        /// </summary>
        /// <returns></returns>
        public ActionResult CourseIndex()
        {
            string userID=Request.Cookies["userInfo"].Values["userID"];
            CourseRepository rep = new CourseRepository();
            IEnumerable<Course> temp = rep.getCourseListForStudent(userID).AsEnumerable();

            List<CourseIcon> model = new List<CourseIcon>();
            foreach (var c in temp)
                model.Add(rep.getCourseIcon(c.cID, userID));
            
            return View("CourseIconOverview",model);
        }

        /// <summary>
        /// Returns the course overview for a selected course
        /// Precondition: The user has clicked the course link in the index
        /// Postcondition: The user has received the course view. 
        /// Author: Bjarmi
        /// Date: 7.5
        /// </summary>
        /// <param name="courseID">The ID of the course in question</param>
        /// <returns>CourseView.cshtml loaded with the chosen course info</returns>
        public ActionResult CourseView(int? courseID)
        {
            string userID = getUserIDFromCookie(); 
            if(courseID.HasValue)
            {
                CourseRepository rep = new CourseRepository();
                Course course = rep.getCourseByID(courseID.Value);
                CourseViewModel model = new CourseViewModel();
                model.course = course;
                int nonNullCourseID = courseID.Value;
                if (isTeacherInCourse(userID, nonNullCourseID))
                {
                    model.isTeacher = true; 
                }
                else
                {
                    model.isTeacher = false; 
                }
                return View(model);
            }
            return RedirectToAction("CourseIndex");
        }

        /// <summary>
        /// Returns the model for a selected course
        /// Precondition: 
        /// Postcondition: The course model has been passed on
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpGet]
        public Course getCourse(int courseID)
        {
            CourseRepository rep = new CourseRepository();
            Course course = rep.getCourseByID(courseID);
            return course; 
        }

        /// <summary>
        /// Returns the edit page for a selected course
        /// Precondition: The user has clicked the edit page for the selected course 
        /// Postcondition: The user has received the edit view for the selected course. 
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpGet]
        public ActionResult editCourse(int courseID)
        {
            CourseRepository rep = new CourseRepository();
            Course course = rep.getCourseByID(courseID);
            return View(course);
        }

        /// <summary>
        /// Returns the edit page for a selected course
        /// Precondition: The user has clicked the edit page for the selected course 
        /// Postcondition: The user has received the edit view for the selected course. 
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpGet]
        public ActionResult editCourseList()
        {
            CourseRepository rep = new CourseRepository();
            List<Course> course = rep.getAllCourses().ToList();
            return View(course);
        }

        /// <summary>
        /// Submits the edit course information to the database. 
        /// Precondition: The user has entered edit course information into the appropriate form
        /// Postcondition: The information has been added to the database 
        /// Author: Krilli 
        /// Date: 8.5
        /// </summary>
        [HttpPost]
        public ActionResult editCourse(Course course)
        {
            CourseRepository rep = new CourseRepository();
            bool success = rep.editCourse(course);
            //These return statements obviously need to be modified when the layout is put together. 
            if (success)
            {
                return View("win");
            }
            else
            {
                return View("fail"); 
            }
        }

        /// <summary>
        /// Adds a level to the selected course
        /// Precondition: The user has clicked the add level button for the course 
        /// Postcondition: The user has received the add level form. 
        /// Author: Krilli 
        /// Date: 8.5
        /// </summary>
        public ActionResult addLevel(int courseID)
        {
            CourseLevel level = new CourseLevel();
            level.cID = courseID; 
            return View(level);
        }
        /// <summary>
        /// Submits the level information to the database and adds a new level 
        /// Precondition: The user has entered level information into the database 
        /// Postcondition: The level has been added to the database 
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpPost]
        public ActionResult addLevel(CourseLevel level)
        {
            CourseLevelRepository rep = new CourseLevelRepository();
            bool success = rep.addLevel(level);
            if (success)
            {
                return View("win");
            }
            else
            {
                return View("fail"); 
            }
        }

        /// <summary>
        /// Returns a list of all courses to add teachers to them. 
        /// Postcondition: The list of courses is returned in the edit view. 
        /// Author: Krilli
        /// Date: 9.5
        /// </summary>
        /// <returns></returns>
        public ActionResult addTeacherToCourseList()
        {
            CourseRepository rep = new CourseRepository();
            List<Course> course = rep.getAllCourses().ToList();
            return View(course);
        }

        /// <summary>
        /// Returns the add teacher to course form 
        /// Precondition: 
        /// Postcondition: The form view has been returned to the user. 
        /// Author: krilli
        /// Date: 11.5 
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public ActionResult addTeacherToCourse(int courseID)
        {
            TeachesClass teaches = new TeachesClass();
            teaches.courseID = courseID;
            return View(teaches); 
        }

        /// <summary>
        /// Submits the user to a course as a teacher.
        /// Precondition: The data has been entered into a form. 
        /// Postcondition: The user has been submitted to the DB as a teacher for a class. 
        /// Author: krilli
        /// Date: 11.5
        /// </summary>
        /// <param name="teaches"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult addTeacherToCourse(TeachesClass teaches)
        {
            CourseRepository rep = new CourseRepository();
            if (rep.addTeacherToCourse(teaches))
            {
                return View("win");
            }
            else
            {
                return View("fail");
            }

        }

        /// <summary>
        /// Returns the teacher operations view.
        /// Postcondition: If the user is a teacher, the teacher operations view is returned. If not, a error page is returned. 
        /// Author: krilli
        /// Date: 11.5
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public ActionResult TeacherView(int courseID)
        {
            string userID = getUserIDFromCookie();
            if (isTeacherInCourse(userID, courseID))
            {
                CourseRepository rep = new CourseRepository();
                Course course = rep.getCourseByID(courseID);
                return View(course); 
            }
            else
            {
                return View("notTeacher"); 
            }
        }
        /// <summary>
        /// Determines if a user is a teacher in a course. 
        /// Precondition: The user exists, the course exists .
        /// Postcondition: A bool value is returned. 
        /// Author: Krill
        /// Date: 11.5
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public bool isTeacherInCourse(string userID, int courseID)
        {
            AccountRepository rep = new AccountRepository();
            bool retVal = rep.isTeacherInCourse(userID, courseID);
            return retVal;
        }

        /// <summary>
        /// The user ID is pulled from the user's cookie
        /// Postcondition: The user ID is returned
        /// Author: krilli
        /// Date: 11.5
        /// </summary>
        /// <returns></returns>
        public string getUserIDFromCookie()
        {
            string userID = Request.Cookies["userInfo"].Values["userID"];

            return userID;
        }

        /// <summary>
        /// Returns the admin view for the course. 
        /// Postcondition: the admin view is returned if the user is a admin. If not, a error page. 
        /// Author: krilli
        /// Date: 11.5
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public ActionResult AdminView(int courseID)
        {
            AccountRepository accRep = new AccountRepository(); 
            string userID = getUserIDFromCookie();
            if (accRep.isAdmin(userID))
            {
                CourseRepository courseRep = new CourseRepository();
                Course course = courseRep.getCourseByID(courseID);
                return View(course);
            }
            else
            {
                return View("notTeacher");
            } 
        }

        /// <summary>
        /// Returns the add student to course form. 
        /// Postcondition: the form is returned to the user.
        /// Author: krilli
        /// Date: 11.5
        /// </summary>
        /// <param name="courseID"></param>
        /// <returns></returns>
        public ActionResult addStudentToCourse(int courseID)
        {
            AddUserToClassModel model = new AddUserToClassModel();
            model.cID = courseID; 
           return View(model); 
        }

        /// <summary>
        /// Adds the student to teh course in the database. 
        /// Precondition: the form has been submitted with the relevant data. 
        /// Postcondition: The user is submitted to the database as a student in that particular course. 
        /// Author: Krilli
        /// Date: 11.5
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult addStudentToCourse(AddUserToClassModel model)
        {
            CourseRepository rep = new CourseRepository();
            if (rep.addStudentToCourse(model))
            {
                return View("win");
            }
            else
            {
                return View("fail"); 
            }
        }

        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        //public ActionResult removeLevel(/*levelID*/)
        //{
        //    return View();
       // }

        [HttpGet]
        public ActionResult createCourse()
        {
            Course course = new Course();

            return View("editCourse", course);
        }

        [HttpPost]
        public ActionResult createCourse(Course course)
        {
            CourseRepository rep = new CourseRepository();

            rep.addCourse(course);

            return View("../Account/Admin");
        }
    }
}
